package com.example.wuyeguanli.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.Response;
import com.example.wuyeguanli.entiy.Room; // 重构: 引入Room实体
import com.example.wuyeguanli.tools.ApiClient;
import com.example.wuyeguanli.tools.DataValidator;

public class FeeQueryActivity extends AppCompatActivity {
    private final String TAG = "ApiClient";
    private Button btnBack;
    private EditText edit_room;
    private TextView elect_num;
    private TextView water_num;
    private TextView management_num;
    private Button btn_submit;
    private ProgressBar loading_progress;
    private final ApiClient apiClient = new ApiClient();

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feequery);
        if(getSupportActionBar() != null) getSupportActionBar().hide();

        btnBack = findViewById(R.id.btn_back);
        edit_room = findViewById(R.id.edit_room);
        elect_num = findViewById(R.id.elect_num);
        water_num = findViewById(R.id.water_num);
        management_num = findViewById(R.id.management_num);
        btn_submit = findViewById(R.id.btn_submit);
        loading_progress = findViewById(R.id.loading_progress);

        btnBack.setOnClickListener(v -> finish());
        btn_submit.setOnClickListener(v -> {
            setLoadingState(true);
            String roomId = edit_room.getText().toString();
            if (!DataValidator.isValidRoomNumber(roomId)) {
                Toast.makeText(FeeQueryActivity.this, "房间号要求6位数字", Toast.LENGTH_SHORT).show();
                setLoadingState(false);
                return;
            }

            // 重构: API调用使用Room实体
            apiClient.getRoomBalances(roomId, new ApiClient.OnCompleteListener<Response<Room>>() {
                @Override
                public void onSuccess(Response<Room> data) {
                    setLoadingState(false);
                    if (data.getState().equals("success") && data.getData() != null) {
                        Room room = data.getData();
                        elect_num.setText(String.valueOf(room.getElectricityBalance()));
                        water_num.setText(String.valueOf(room.getWaterBalance()));
                        management_num.setText(String.valueOf(room.getPropertyFeeBalance()));
                        Toast.makeText(FeeQueryActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                    } else {
                        clearBalances();
                        Toast.makeText(FeeQueryActivity.this, data.getError() != null ? data.getError() : "查询失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void OnError(Throwable error) {
                    setLoadingState(false);
                    clearBalances();
                    Log.e(TAG, "OnError: ", error);
                    Toast.makeText(FeeQueryActivity.this, "网络错误，请稍后重试", Toast.LENGTH_SHORT).show();
                }
            });
        });
    }

    private void clearBalances() {
        elect_num.setText("");
        water_num.setText("");
        management_num.setText("");
    }

    private void setLoadingState(boolean isLoading) {
        loading_progress.setVisibility(isLoading ? View.VISIBLE : View.GONE);
        edit_room.setEnabled(!isLoading);
        btn_submit.setEnabled(!isLoading);
    }
}